package org.codert.wxpay.support;

import org.codert.wxpay.support.client.WxPayClient;
import org.codert.wxpay.support.exception.NonExistentException;
import org.springframework.beans.factory.ObjectProvider;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: TanJingyu
 * @create: 2021-08-13 11:20
 **/
public class WxPayClientFactory {
    private ObjectProvider<WxPayClient> wxPayClients;

    public WxPayClientFactory(ObjectProvider<WxPayClient> wxPayClients) {
        this.wxPayClients = wxPayClients;
    }

    public WxPayClient getWxPayClient(ConstantEnum payWay) {
        List<WxPayClient> clients = this.wxPayClients.stream().collect(Collectors.toList());
        for (WxPayClient client : clients) {
            if (client.support(payWay)) {
                return client;
            }
        }
        throw new NonExistentException(String.format("未找到支付方式为【%s】的client", payWay.getMeaning()));
    }
}
